<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="居民" prop="peopleName">
                <el-input
                    v-model="queryParams.peopleName"
                    placeholder="请输入名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="核销码" prop="verificationCode">
                <el-input
                    v-model="queryParams.verificationCode"
                    placeholder="请输入核销码"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="优惠券" prop="title">
                <el-input
                    v-model="queryParams.title"
                    placeholder="请输入优惠券"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="使用状态"
                    clearable
                    style="width: 240px"
                >
                    <el-option
                        v-for="dict in dict.type.user_coupon_status"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['business:peopleCoupon:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="peopleCouponList" @selection-change="handleSelectionChange" border>
            <el-table-column type="selection" width="55" align="center"/>
            <!--<el-table-column label="来源券码" align="center" prop="sourceId"/>
            <el-table-column label="居民" align="center" prop="peopleName"/>
            <el-table-column label="村社" align="center" prop="communityName"/>-->
            <el-table-column label="优惠券" align="left" prop="title" min-width="180" :show-overflow-tooltip="true"/>
            <!--<el-table-column label="券码" align="center" prop="code"/>-->
            <el-table-column label="类型" align="center" prop="couponType">
                <template slot-scope="scope">
                    <el-tag  v-if="scope.row.couponType==0">满减</el-tag>
                    <el-tag type="warning" v-if="scope.row.couponType==1">打折券</el-tag>
                    <el-tag type="danger" v-if="scope.row.couponType==2">兑换券</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="规则" align="center" prop="amount" :show-overflow-tooltip="true" width="200">
                <template slot-scope="scope">
                    满{{scope.row.amount}},
                    <span v-if="scope.row.couponType==0">减{{scope.row.reduce}}</span>
                    <span v-if="scope.row.couponType==1">打{{scope.row.discount}}折</span>
                    <span v-if="scope.row.couponType==2">兑换{{scope.row.exchangeItems}}</span>
                </template>
            </el-table-column>
            <el-table-column label="兑换时间" align="center" prop="receivedTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.receivedTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <el-tag type="danger" v-if="scope.row.status==0">待使用</el-tag>
                    <el-tag type="success" v-if="scope.row.status==1">已使用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="核销码" align="center" prop="verificationCode"/>
            <el-table-column label="核销门店" align="center" prop="verificationStore"/>
            <el-table-column label="核销时间" align="center" prop="verificationTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.verificationTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="消耗积分" align="center" prop="score"/>
<!--            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" style="display: none">-->
<!--                <template slot-scope="scope">-->
<!--                    <el-button-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-view"-->
<!--                        @click="handleUpdate(scope.row)"-->
<!--                        v-hasPermi="['business:peopleCoupon:edit']"-->
<!--                    >查看-->
<!--                    </el-button>-->
<!--                </template>-->
<!--            </el-table-column>-->
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改用户券码对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="来源券码" prop="sourceId">
                    <el-input v-model="form.sourceId" placeholder="请输入来源券码"/>
                </el-form-item>
                <el-form-item label="用户ID" prop="peopleId">
                    <el-input v-model="form.peopleId" placeholder="请输入用户ID"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="peopleName">
                    <el-input v-model="form.peopleName" placeholder="请输入${comment}"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="communityId">
                    <el-input v-model="form.communityId" placeholder="请输入${comment}"/>
                </el-form-item>
                <el-form-item label="村社" prop="communityName">
                    <el-input v-model="form.communityName" placeholder="请输入村社"/>
                </el-form-item>
                <el-form-item label="优惠券标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入优惠券标题"/>
                </el-form-item>
                <el-form-item label="券码" prop="code">
                    <el-input v-model="form.code" placeholder="请输入券码"/>
                </el-form-item>
                <el-form-item label="满多少金额" prop="amount">
                    <el-input v-model="form.amount" placeholder="请输入满多少金额"/>
                </el-form-item>
                <el-form-item label="减多少金额" prop="reduce">
                    <el-input v-model="form.reduce" placeholder="请输入减多少金额"/>
                </el-form-item>
                <el-form-item label="打多少折" prop="discount">
                    <el-input v-model="form.discount" placeholder="请输入打多少折"/>
                </el-form-item>
                <el-form-item label="兑换物" prop="exchangeItems">
                    <el-input v-model="form.exchangeItems" placeholder="请输入兑换物"/>
                </el-form-item>
                <el-form-item label="券有效期: 开始时间" prop="start">
                    <el-date-picker clearable
                                    v-model="form.start"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择券有效期: 开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="券有效期：结束时间" prop="end">
                    <el-date-picker clearable
                                    v-model="form.end"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择券有效期：结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="核销码" prop="verificationCode">
                    <el-input v-model="form.verificationCode" placeholder="请输入核销码"/>
                </el-form-item>
                <el-form-item label="核销门店" prop="verificationStore">
                    <el-input v-model="form.verificationStore" placeholder="请输入核销门店"/>
                </el-form-item>
                <el-form-item label="核销时间" prop="verificationTime">
                    <el-date-picker clearable
                                    v-model="form.verificationTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择核销时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="兑换时间" prop="receivedTime">
                    <el-date-picker clearable
                                    v-model="form.receivedTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择兑换时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="兑换消耗的积分" prop="score">
                    <el-input v-model="form.score" placeholder="请输入兑换消耗的积分"/>
                </el-form-item>
                <el-form-item label="券使用说明" prop="description">
                    <el-input v-model="form.description" placeholder="请输入券使用说明"/>
                </el-form-item>
                <el-form-item label="可用时间段" prop="timeInteral">
                    <el-input v-model="form.timeInteral" placeholder="请输入可用时间段"/>
                </el-form-item>
                <el-form-item label="可用门店" prop="stores">
                    <el-input v-model="form.stores" placeholder="请输入可用门店"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listPeopleCoupon,
    getPeopleCoupon,
    delPeopleCoupon,
    addPeopleCoupon,
    updatePeopleCoupon
} from "@/api/business/peopleCoupon";

export default {
    name: "PeopleCoupon",
    dicts: ['user_coupon_status'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户券码表格数据
            peopleCouponList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sourceId: null,
                peopleId: null,
                peopleName: null,
                communityId: null,
                communityName: null,
                title: null,
                code: null,
                couponType: null,
                amount: null,
                reduce: null,
                discount: null,
                exchangeItems: null,
                start: null,
                end: null,
                status: null,
                verificationCode: null,
                verificationStore: null,
                verificationTime: null,
                receivedTime: null,
                score: null,
                description: null,
                timeInteral: null,
                stores: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询用户券码列表 */
        getList() {
            this.loading = true;
            listPeopleCoupon(this.queryParams).then(response => {
                this.peopleCouponList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                userCouponId: null,
                sourceId: null,
                peopleId: null,
                peopleName: null,
                communityId: null,
                communityName: null,
                title: null,
                code: null,
                couponType: null,
                amount: null,
                reduce: null,
                discount: null,
                exchangeItems: null,
                start: null,
                end: null,
                status: null,
                verificationCode: null,
                verificationStore: null,
                verificationTime: null,
                receivedTime: null,
                score: null,
                description: null,
                timeInteral: null,
                stores: null,
                createTime: null,
                updateTime: null,
                createBy: null,
                updateBy: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.userCouponId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加用户券码";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const userCouponId = row.userCouponId || this.ids
            getPeopleCoupon(userCouponId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改用户券码";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.userCouponId != null) {
                        updatePeopleCoupon(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addPeopleCoupon(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const userCouponIds = row.userCouponId || this.ids;
            this.$modal.confirm('是否确认删除用户券码编号为"' + userCouponIds + '"的数据项？').then(function () {
                return delPeopleCoupon(userCouponIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/peopleCoupon/export', {
                ...this.queryParams
            }, `券码兑换记录_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
